<?php
class AreauserAction extends BaseAction {
	function _initialize() {
		parent::_initialize();
		//申请类型 1省专营商2市专营商3区专营商4代理商
		$agent_stype = array(1=>'省专营商',2=>'市专营商',3=>'区专营商',4=>'代理商');
		$this->assign('agent_stype',$agent_stype);
		//支付状态 0未付款 1线上付款失败 2线下付款失败 3付款中 4线上已付款 5线下已付款
		$agent_pay_status = array(0=>'未付款',1=>'线上付款失败',2=>'线下付款失败',3=>'付款中',4=>'线上已付款',5=>'线下已付款');
		$this->assign('agent_pay_status',$agent_pay_status);
		//处理状态 0未审核 1审核未通过 2审核通过
		$agent_status = array(0=>'未审核',1=>'审核未通过',2=>'审核通过');
		$this->assign('agent_status',$agent_status);
		//开通状态 0未开通 1关闭帐户 2开通帐户
		$agent_isclose = array(0=>'未开通',1=>'关闭帐户',2=>'开通帐户');
		$this->assign('agent_isclose',$agent_isclose);
		//操作类型：1申请 2付款 3审核 4状态 5修改
		$log_type = array(1=>'申请',2=>'付款  ',3=>'审核 ',4=>'状态',5=>'修改');
		$this->assign('log_type',$log_type);
		
		$pro_type = array();
		$pro_type['sj'] = '手机';
		$pro_type['pb'] = '平板';
		$pro_type['sjpj'] = '手机配件';
		$pro_type['sjsp'] = '手机饰品';
		$pro_type['sjlh'] = '手机靓号';
		$pro_type['hmpf'] = '号码批发';
		$this->assign('pro_type',$pro_type);
		
		$area=F('Area');
        if(empty($area)){
            $area=M('Area')->select();
            $area=field2key($area,'id');
            F('Area',$area);
        }

        $this->assign('Area_list',$area);
	}
	/*****************************
	*@区域会员显示
	*****************************/	
  	public function index(){
		$money = M('user_money');
  		$condition = array();
		$wherefields = array();
		//商户区域条件查询
		$vesting_province= intval($_REQUEST['vesting_province']);
		if ($vesting_province){
			$where['province'] = $vesting_province;
		}
		$vesting_city= intval($_REQUEST['vesting_city']);
		if ($vesting_city){
			$where['city'] = $vesting_city;
		}
		$vesting_area= intval($_REQUEST['vesting_area']);
		if ($vesting_area){
			$where['area'] = $vesting_area;
		}
		$status = $_REQUEST['status'];
		if($status != "all" && !empty($status)){
			$where['b.status'] = $status;
		}
		$key = $_REQUEST['keywords'];
		$stype = $_REQUEST['sfield'];
		
		$wherefields = $stype;
		if(!empty($key) && $stype!='all'){
			$where[$stype] = array('like',"%{$key}%");
		}

		//$count = $Obj->where($condition)->join('as b inner join user as u ON b.user_id = u.id join user_money as m ON m.userid = b.user_id')->count();
		$count = $money->where($where)->join('AS m INNER JOIN user_b2b AS b ON m.userid=b.user_id INNER JOIN user_type AS t ON m.userid = t.user_id INNER JOIN `user` AS u ON t.user_id = u.id')->count();
		$Page = new Page( $count,$this->listrows);
		//$res=$Obj->join('as b inner join user as u ON b.user_id = u.id join user_money as m ON m.userid = b.user_id')->group('u.id desc')->where($condition)->limit($Page->firstRow.','.$Page->listRows)->select();
		$res = $money->field('m.userid,m.subject,m.value,b.realname,b.realname,b.company,b.id,b.user_id,b.mobile,b.status,b.address,u.username,u.email,u.createtime,b.province,b.city,b.area')->join('AS m INNER JOIN user_b2b AS b ON m.userid=b.user_id INNER JOIN user_type AS t ON m.userid = t.user_id INNER JOIN `user` AS u ON t.user_id = u.id')->where($where)->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($res as $value){	
			$data[$value['userid']]['userid'] =$value['userid'];
			 if($value['subject'] == 'CV'){
				$data[$value['userid']]['cv'] =$value['subject'];
				$data[$value['userid']]['cvvalue'] =$value['value'];
			 }
			if($value['subject'] == 'BV'){
				$data[$value['userid']]['bv'] =$value['subject'];
				$data[$value['userid']]['bvvalue'] =$value['value'];
			 }
			if($value['subject'] == 'PV'){
				$data[$value['userid']]['pv'] =$value['subject'];
				$data[$value['userid']]['pvvalue'] =$value['value'];
			 }
			if($value['subject'] == 'MV'){
				$data[$value['userid']]['mvvalue'] =$value['value'];
			 }
			$data[$value['userid']]['realname'] =$value['realname'];	
			$data[$value['userid']]['company'] =$value['company'];
			$data[$value['userid']]['mobile'] =$value['mobile'];
			$data[$value['userid']]['address'] =$value['address'];
			$data[$value['userid']]['username'] =$value['username'];
			$data[$value['userid']]['email'] =$value['email'];
			$data[$value['userid']]['createtime'] =$value['createtime'];
			$data[$value['userid']]['province'] =$value['province'];
			$data[$value['userid']]['city'] =$value['city'];
			$data[$value['userid']]['area'] =$value['area'];
			$data[$value['userid']]['status'] =$value['status'];
			$data[$value['userid']]['id'] =$value['id'];
			$data[$value['userid']]['user_id'] =$value['user_id'];
			$data[$value['userid']]['realname'] =$value['realname'];				
		}
		if(!empty($condition['province'])){
			 $condition['province'] = $condition['province'];
		}
		if(!empty($condition['city'])){
			$condition['city'] = $condition['city'];
		}
		if(!empty($condition['area'])){
			$condition['area'] = $condition['area'];
		}
		$this ->assign($_REQUEST);
		$this->assign('wherefields',$wherefields);	
		$this->assign('status',$status);
		$this->assign('list',$data);
		$this->assign('totalCount',$count);
		$this->assign($condition);
		$this->display();
	}	
	
	/**********************
	*@区域会员订单显示
	***********************/
	public function order(){
		//商户区域条件查询
		$array = array();
		$key  = $_REQUEST['keywords'];
		$stype = $_REQUEST['stype'];
		$ktime = $_REQUEST['ktime'];
		$jtime = $_REQUEST['jtime'];
		$statime = strtotime($ktime);
		$shoptime = strtotime($jtime);
		//$where['o.platform'] = "SMW01";
		$where['o.status'] = array('NEQ','-10');
		if($stype == 'agent_id' && !empty($key)){
			$where['agent_id'] = $key;
		}
		if(!empty($statime) && empty($shoptime)){
				$where['add_time'] = array('egt',$statime);
		}elseif(!empty($statime) && !empty($shoptime)){
				$where['add_time'] = array(array('egt',$statime),array('elt',$shoptime),'and');
		}
		$vesting_province= intval($_REQUEST['vesting_province']);
		if(!empty($key) && $stype!='all'){
			$where[$stype] = array('like',"{$key}%");
		}
		$model = M();
		//$count = $model->where($where)->table('user_type ut')->join('right join `order` o ON ut.user_id = o.userid ')->join('right join `user` u ON ut.user_id = u.id')->group('o.sn')->select();
		$count = $model->where($where)->table('user_type ut')->join('left join `order` o ON ut.user_id = o.userid ')->join('left join `user` u ON ut.user_id = u.id')->join('left join `order_ext` as e on e.order_id = o.id')->group('o.sn')->select();
		$count = count($count);
		$Page = new Page( $count,$this->listrows);
		//$res = $model->where($where)->limit($Page->firstRow,$Page->listRows)->field('ut.agent_id,ut.user_id,o.id,o.sn,o.amount,o.status,o.pay_status,o.shipping_status,o.add_time,u.username')->table('user_type ut')->join('right join `order` o ON o.userid = ut.user_id')->join('right join `user` u ON u.id = ut.user_id')->join('right join `order_ext` as e on e.order_id = o.id')->group('o.sn')->order('o.add_time DESC')->select();  
		$res = $model->where($where)->limit($Page->firstRow,$Page->listRows)->field('ut.agent_id,ut.user_id,o.id,o.sn,o.amount,o.status,o.pay_status,o.shipping_status,o.add_time,u.username')->table('user_type ut')->join('left join `order` o ON ut.user_id = o.userid')->join('left join `user` u ON ut.user_id = u.id')->join('left join `order_ext` as e on e.order_id = o.id')->group('o.sn')->order('o.add_time DESC')->select();  
		$this->assign('totalCount',$count);	
		$this->assign('vesting_province',$array['province']);
		$this->assign('vesting_city',$array['city']);
		$this->assign('vesting_area',$array['area']);
		$this->assign('ktime',$statime);
		$this->assign('jtime',$shoptime);
		$this->assign($condition);
		$this->assign('stype',$stype);
		$this->assign('list',$res);
		$this->display();
		
	}
	public function sel(){
	
		$sel = M('order');
		$id = $_REQUEST['id'];
		if(empty($id)){
			return " ";
		}
		$array = array();
		$array['o.id'] = $id;
		$result = $sel->join('as o inner join user_b2b as b ON o.userid=b.user_id')->limit(1)->where($array)->select();
		$this->assign('info',$result[0]);
		$this->display();
	}
	
	public function edit(){
	
		$useredit = M('user_b2b');
		$array = array();
		$id = $_REQUEST['id'];
		if(empty($id)){
			return " ";
		}
		$array['user_id'] = $id;
		$res=$useredit->field('u.username,u.email,u.mobile,u.status,b.id,b.user_id,b.createtime,b.realname,b.tel,b.mobile,b.province,b.city,b.area,b.address')->join('as b inner join user as u ON b.user_id = u.id')->where($array)->select();
		$this->assign('info',$res[0]);
		$this->display();
	}
	
	/*********************
	*区域会员修改
	**********************/	
	public function update(){
	
		$Ouser = M('user_b2b');
		$agent = M('user_agent');
		$type = M('user_type');
		$array = array();
		$where = array();
		$id = $_REQUEST['id'];
		if(empty($id)){
				$this->_ajm(300,'请选择一条数据修改',0);
		}
		if($_REQUEST['vesting_province'] != 0 && !empty($_REQUEST['vesting_province'])){
				$array['province'] = $_REQUEST['vesting_province'];
		}
	   $vesting_city = $_REQUEST['vesting_city'];
		if($vesting_city){
				$array['city']  = $vesting_city;
		}
		$vesting_area = $_REQUEST['vesting_area'];
		if($vesting_area){
				$array['area'] = $vesting_area ;
		}
		if(!empty($_REQUEST['address'])){
				$array['address'] = $_REQUEST['address'];
		}
		if(!empty($_REQUEST['tel'])){
				$array['tel'] = $_REQUEST['tel'];
		}
		if(!empty($_REQUEST['mobile'])){
				$array['mobile'] = $_REQUEST['mobile'];
		}
		$user = array();
		//获取省级专营商id
		if($_REQUEST['vesting_province'] != '0'){
		
				$where['vesting_province'] = $_REQUEST['vesting_province'];
				$where['vesting_city'] = $_REQUEST['vesting_city'];
				$where['vesting_area'] = $_REQUEST['vesting_area'];	
				$result = $agent->query("select * from user_agent where vesting_province=".$where['vesting_province']);
				$user[0] = $result[0]['user_id'];	
		}
		//获取市级专营商id
		if($_REQUEST['vesting_province'] != '0' && $_REQUEST['vesting_city'] !='0'){
				$res = $agent->query("select * from user_agent where vesting_province={$where['vesting_province']} and vesting_city=".$where['vesting_city']);
				$user[1]=$res[0]['user_id'];
		}
		//获取区级专营商id
		if($_REQUEST['vesting_province'] != '0' && $_REQUEST['vesting_city'] !='0' && $_REQUEST['vesting_area'] !='0'){
				$value = $agent->query("select * from user_agent where vesting_province={$where['vesting_province']} and vesting_city={$where['vesting_city']} and vesting_area=".$where['vesting_area']);
				$user[2] = $value[0]['user_id'];
		}
		//修改用户区域归属地
		$count=$Ouser->where("user_id={$id}")->save($array);
		if($count>0){
		//关联表用户关联经销商
			$res = $type->where('user_id='.$id)->select();
			if(empty($user[1])){
					$user[1] = 0;
					$user[2] = 0;
			}
			if(empty($user[2])){
					$user[2] = 0;	
			}
			if($res!='null'){
				for($i = 0;$i<count($res);$i++){
						$count = $type->query("UPDATE `user_type` SET agent_id={$user[$i]} WHERE id=".$res[$i]['id']);
				}		
					$this->_ajm(200,'修改成功',1);
			}
			
		}else{
				$this->_ajm(300,'修改失败',0);
		}
	}
	/********************
	*会员资产显示
	*********************/
/*	public function money(){
	
		$money = M('user_money');
		$array = array();
		$array['platform'] = "SMW01";
		$key  = $_REQUEST['keywords'];
		$stype = $_REQUEST['stype'];
		if(!empty($key) && $stype!='all'){
			$array[$stype] = array('like',"%{$key}%");
		}
		 $sub = $_REQUEST['sub'];
		 if($sub != 'all' && $sub != null){
			$array['subject'] = $sub;
		 }
		 
		if($stype == 'agent_id'){
			$count = $money->where($array)->join('as m inner join user_type as t ON m.userid=t.user_id')->count();
			$Page = new Page( $count,$this->listrows);	
			$result = $money->join('as m inner join user_type as t ON m.userid=t.user_id')->where($array)->limit($Page->firstRow.','.$Page->listRows)->select();
		}else{
			$count = $money->where($array)->join('as m inner join user_b2b as b ON m.userid=b.user_id ')->count();
			$Page = new Page( $count,$this->listrows);
			$result = $money->where($array)->limit($Page->firstRow.','.$Page->listRows)->join('as m inner join user_b2b as b ON m.userid=b.user_id ')->select();
		}

		$this->assign('status',$sub);
		$this->assign('stype',$stype);
		$this->assign('list',$result);
		$this->assign('totalCount',$count);	
		$this->display();
	}
*/

	 public function money(){
	 
		$assets = M('user_assets');
		$where = array();
		//$where['platform'] = "SMW01";
		$key  = $_REQUEST['keywords'];
		$stype = $_REQUEST['stype'];
		$sub = $_REQUEST['sub'];
		if(!empty($key) && $stype!='all'){
				$where[$stype] = array('like',"%{$key}%");
		}
		if($sub != 'all' && $sub != null){
				$where['subject'] = $sub;
		 }
		//$where['agent_id'] = $this->_userid;
		$statime = strtotime($_REQUEST['ktime']);
		$shoptime = strtotime($_REQUEST['jtime']);
		$uname  = $_REQUEST['uname'];
		$sfield = $_REQUEST['sfield'];
		if($_REQUEST['type'] != 'all' && !empty($_REQUEST['type'])){
				$where['changtype'] = $_REQUEST['type'];
		}
		if($_REQUEST['subject'] != 'all' && !empty($_REQUEST['subject'])){
				$where['subject'] = $_REQUEST['subject'];
		}
		if(!empty($statime) && empty($shoptime)){
				$where['ctime'] = array('egt',$statime);
		}elseif(!empty($statime) && !empty($shoptime)){
				$where['ctime'] = array(array('egt',$statime),array('elt',$shoptime),'and');
		}
		if(!empty($uname) && $sfield!='all'){
				$where[$sfield] = array('like',"%{$uname}%");
		}
		$count = $assets->where($where)->join('AS s INNER JOIN user_type AS t ON s.userid= t.user_id INNER JOIN `user` AS u ON u.id = t.user_id')->count();
		$Page = new Page( $count,$this->listrows);	
		$result = $assets->field('s.platform,s.cip,s.id,s.subject,s.changtype,s.value,s.nowvalue,s.bzvu,s.ctime,t.user_id,u.username')->join('AS s INNER JOIN user_type AS t ON s.userid= t.user_id INNER JOIN `user` AS u ON u.id = t.user_id')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('s.ctime DESC')->select();
		$this->assign('status',$sub);
		$this->assign('stype',$stype);
		$this->assign('datetime',$statime);
		$this->assign('endtime',$shoptime);
		$this->assign('list',$result);
		$this->assign('totalCount',$count);	
		$this->display();
		
	 }
	 
	 /*
	 *@一键更新
	 */
	 public function update_real_pay(){
		import("Com.KitMoney");
		$kitmoney = new KitMoney();
		$order = M('order');
		$agent_id = $_GET['agent_id'];
		if( !empty ($agent_id) ){
			$res = $order->join('as o inner join user_type as t ON o.userid = t.user_id')->field('o.sn')->where('t.agent_id='.$agent_id)->select();
		} else {
			$where = array();
			//$where['platform'] = 'SMW01';
			//$where['agent_id'] = array('EQ',100965);
			//$where['sn'] = array('EQ',1014022111780);
			$day = intval($_REQUEST['day']);
			if(!empty($day)){
				$nowtime=date("Y-m-d H:i:s");
				$oldtime=strtotime("$nowtime-".$day." day");
				$where['add_time'] = array('GT',$oldtime);
			}
			$res = $order->field('sn')->where($where)->select();
		}
		foreach ($res as $key => $value) {
			$order=$kitmoney->order_statis($value['sn']);
		}
		if($order != false){
			$this->_ajm(200,'更新成功',1,U ( 'Areauser/profit' ) );
		}else{
			$this->_ajm(300,'更新失败',1);
		}
			
	}
	 
	 /*
	 *@一键更新番禺区会员订单实际支付
	 */
	 public function update_real_pay_panyu(){
		import("Com.KitMoney");
		$kitmoney = new KitMoney();
		//$order = M('order');
		$where = array();
		//$where['platform'] = 'SMW01';
		$where['agent_id'] = array('EQ',11871);//11871 100965
		//$where['sn'] = array('EQ',10140416106890);
		$day = intval($_REQUEST['day']);
		if(!empty($day)){
			$nowtime=date("Y-m-d H:i:s");
			$oldtime=strtotime("$nowtime-".$day." day");
			$where['o.add_time'] = array('GT',$oldtime);
		}
		//$res = $order->field('sn')->where($where)->select();
		$model = M();
		$res = $model->field('o.sn,og.order_id,og.product_id,og.price,og.priceall,og.number,og.mname')->where($where)->table('user_type ut')->order('add_time desc')->join('right join `order` o ON o.userid = ut.user_id')->join('right join `order_goods` og ON og.order_id = o.id')->select();
		//dump($res);exit;
		$order = $this->actual_pay_order_panyu($res);
		/*foreach ($res as $key => $value) {
			//$order=$kitmoney->order_statis($value['sn']);
		}*/
		if($order != false){
			$this->_ajm(200,'更新成功',1,U ( 'Areauser/profit2' ) );
		}else{
			$this->_ajm(300,'更新失败',1);
		}		
	}
	 
	private function actual_pay_order_panyu($data = array()){
		if(empty($data)){
				return false;
		}
		$finance = M('order_finance');			
		$paypa = M('order_real_pay_panyu');
		$total = array();
		foreach ($data as $key => $value) {
			$total[$key] = $value;
			//查询缺货退款总
			$where['goods_id'] = $value['product_id'];
			$where['order_id'] = $value['order_id'];
			$where['subject'] = 'CV';
			//$where['platform'] = 'SMW01';
			$where['action'] = array(array('eq','OOS'),array('eq','REFUND'),'OR'); 
			$where['status'] = '1';
			$temp_finance = $finance->field('SUM(`value`*`number`) as money,SUM(`number`) as num')->where($where)->find();
			$total[$key]['priceall'] = $total[$key]['priceall'] - $temp_finance['money'];
			$total[$key]['number'] = $total[$key]['number'] - $temp_finance['num'];
		}
		$ret = 0;
		//dump($total);
		$tdata=array();
		foreach ($total as $key => $value) {
			/*if(empty($value['mname']) || $value['mname'] ==''){
				$value['mname'] = 'pj';
			}*/
			
			$tdata['order_id'] = $value['order_id']; //订单编号
			$tdata['product_id'] = $value['product_id'];
			$tdata['price'] = $value['price'];
			$tdata['number'] = $value['number'];
			$tdata['priceall'] = $value['priceall'];
			$tdata['action_userid'] = $this->userid;
			$tdata['mname'] = $value['mname']; //商品类型
			$tdata['ctime'] = time();
			//计算成本利润
			if($value['priceall']>0){
				$value['priceall']=$value['priceall']/100;
				$chengben = 0;
				$maolirun = 0;
				$lirun = 0;	
				
				if($value['mname']=='sj'||$value['mname']=='pb'){
	                $chengben = $value['priceall']-$value['priceall']*0.02;
					$maolirun = $value['priceall']*0.02;
					$lirun = $value['priceall']*0.02*0.3;
					$tdata['cost_price']=$chengben*100;
					$tdata['gross_profit']=$maolirun*100;
                	$tdata['profit']=$lirun*100;						
	            }else{
	                $chengben = $value['priceall']-$value['priceall']*0.07;
					$maolirun = $value['priceall']*0.07;
					$lirun = $value['priceall']*0.07*0.3;
					$tdata['cost_price']=$chengben*100;
					$tdata['gross_profit']=$maolirun*100;
                	$tdata['profit']=$lirun*100;
	            }
			}else{
			
				$tdata['cost_price']=0;
				$tdata['gross_profit']=0;
            	$tdata['profit']=0;
				
			}
			
			//$Verification['mname'] = $value['mname'];
			$Verification['order_id'] = $value['order_id'];
			$Verification['product_id'] = $value['product_id'];
			$realpayinfo = $paypa->where($Verification)->select(); //防止重复添加
			
			$tdata['id'] = $realpayinfo[0]['id'];
			$ret = $paypa->add($tdata,array(),true);
			//dump($paypa->getLastSql());
		}
		return $ret;
	}
	 
	/***************************
	*批量关联 分销商区域会员
	****************************/
	public function Address(){
	
		$array = array();
		$agent  =array();
		$agent_u_id = array();
		$user = M('user_agent');
		$b2b = M('user_b2b');
		$u_agent  = M('user_type');
		//获取专营商用户
		$res = $user->select();
		//获取分类用户
		 foreach ($res as $value){
			$array['province'] = $value['vesting_province'];
			if($value['vesting_city'] != 0 ){
					$array['city'] = $value['vesting_city'];
			}
			if($value['vesting_area'] != 0 ){
					$array['area'] = $value['vesting_area'];
			}
			$agent_u_id['agent_id'] = $value['user_id'];
				//获取专营商用户下面的区域用户
			$result = $b2b->field('user_id,province,city,`area`')->where($array)->select();
			 foreach ($result as $va){
						$agent_u_id['user_id'] = $va['user_id'];
						$agentvalue  = $u_agent->where($agent_u_id)->select();
					if(!$agentvalue){
							$u_agent->query("insert into user_type(agent_id,user_id) values ({$value['user_id']},{$va['user_id']})");
				}		
			}
			unset($array);	 
		}
	}
	
	/**********************
	*专营商会员订单统计分成
	***********************/	
   	public function ordersett(){
   
		$aaoc = M('aaoc');
		$array = array();
		$stype = $_REQUEST['stype'];
		$key = $_REQUEST['keywords'];
		$jstype = $_REQUEST['jstype'];
		if($jstype != null && $jstype != 'all'){
				$array['type'] = $jstype;
		}
		if($stype != 'all' && !empty($key)){
				$array['agent_id'] = $key;
		}
		$count = $aaoc->where($array)->count();
		$Page = new Page( $count,$this->listrows);	
		$result = $aaoc->where($array)->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('status',$jstype);
		$this->assign('list',$result);
		$this->assign('stype',$stype);
		$this->assign('totalCount',$count);	
		$this->display();
		
   }
	/**********************
	*分销商关联区域会员修改
	**********************/
	public function sel_user(){
		$where = array();
		$id = $_REQUEST['id'];
		if(empty($id)){
				return " ";
		}
		$where['user_id'] = $id;
		$type =	M('user_type');
		$result = $type->where($where)->select();			
		$this->display();
	}
	

	public function jiesuan(){
	
		$id = $_REQUEST['id'];
		if(empty($id)){
				return false;
		}
		if(!empty($_REQUEST['starttime'])){
				$starttime = $_REQUEST['starttime'];
		}
		if(!empty($_REQUEST['endtime'])){
				$endtime = $_REQUEST['endtime'];
		}
		$res = parent::information($id,$starttime,$endtime);
		if($res == true){
				$this->_ajm(200,'结算成功',1);
		}else{
				$this->_ajm(300,'结算失败',1);
		}
		
	}
	
	/**********************************
	*@订单利润明细
	***********************************/
	public function profit(){
	
		$user =M('user');
		$where = array();
		$statime = strtotime($_REQUEST['ktime']);
		$shoptime = strtotime($_REQUEST['jtime']);
		$uname  = $_REQUEST['uname'];
		$sfield = $_REQUEST['sfield'];
		$status = $_REQUEST['status'];
		if($sfield == 'agent_id' && !empty($uname)){
			$where['agent_id'] = $uname;
		}
		//获取所有专营商下面的商品类型的分成比例
		$user_agent_profit = $this->price_profit($where['agent_id']);
		
		$agent_profit_list = array();
		foreach ($user_agent_profit as $key => $value) {
			$agent_profit_list[$value['agent_id']][$value['mname']] = $value['profit'];
		}

		unset($user_agent_profit);
		$where['o.status'] = array('NEQ','-10');
		if($status !=null && $status != 'all'){
				$where['o.status'] = array(array('EQ',$status),array('NEQ','-10'),'and'); ;
		}
		//$where['o.platform'] = array('EQ','SMW01');
		
		if(!empty($statime) && empty($shoptime)){
				$where['add_time'] = array('egt',$statime);
		}elseif(!empty($statime) && !empty($shoptime)){
				$where['add_time'] = array(array('egt',$statime),array('elt',$shoptime),'and');
		}
		if(!empty($uname) && $sfield!='all'){
				$where[$sfield] = array('like',"%{$uname}%");
		}
		$count = $user->join('AS u INNER JOIN user_type AS t ON u.id=t.user_id INNER JOIN `order` AS o ON u.id = o.userid INNER JOIN `order_real_pay` AS p ON o.id=p.order_id')->where($where)->count();
		$Page = new Page( $count,$this->listrows);
		$result = $user->field('u.id,o.id as oid,t.agent_id,o.status,o.pay_status,o.shipping_status,u.username,o.sn,o.amount,p.mname,p.value,o.add_time,p.mname')->join('AS u INNER JOIN user_type AS t ON u.id=t.user_id INNER JOIN `order` AS o ON u.id = o.userid INNER JOIN `order_real_pay` AS p ON o.id=p.order_id')->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('o.add_time DESC')->select();
		//dump($user->getLastSql());
		$order_list = array();
		foreach ($result as $key => $value) {
			$order_list[$key] = $value;
			$profit = $agent_profit_list[$value['agent_id']][$value['mname']];
			$order_list[$key]['profit'] = $profit;
			$order_list[$key]['profit_total'] = (($value['value']/100) * $profit);
		}
		unset($result);
		$show  = $Page->show();
		$user = 'user_profit';
		$this->assign($_REQUEST);
		$this->assign('datetime',$statime);
		$this->assign('endtime',$shoptime);
		$this->assign('uname',$uname);
		$this->assign('wherefields',$sfield);
		$this->assign('subject',$status);
		$this->assign('list',$order_list);
		$this->assign('totalCount',$count);	
		$this->assign('cur_sidebar',$user);
		$this->display();
		
	}
	
	/**********************************
	*@订单利润明细
	***********************************/
	public function profit2(){
	
		$user =M('user');
		$where = array();
		$statime = strtotime($_REQUEST['ktime']);
		$shoptime = strtotime($_REQUEST['jtime']);
		//$uname  = $_REQUEST['uname'];
		$sfield = $_REQUEST['sfield'];
		//$status = $_REQUEST['status'];
		/*if($sfield == 'agent_id' && !empty($uname)){
			$where['agent_id'] = $uname;
		}*/
		//获取所有专营商下面的商品类型的分成比例
		/*$user_agent_profit = $this->price_profit($where['agent_id']);
		
		$agent_profit_list = array();
		foreach ($user_agent_profit as $key => $value) {
			$agent_profit_list[$value['agent_id']][$value['mname']] = $value['profit'];
		}

		unset($user_agent_profit);*/
		$where['agent_id'] = array('EQ',11871);//11871 100965
		//$where['sn'] = array('EQ',1014022111780);
		$where['o.status'] = array('NEQ','-10');
		if($status !=null && $status != 'all'){
				$where['o.status'] = array(array('EQ',$status),array('NEQ','-10'),'and');
		}
		
		//$where['o.platform'] = array('EQ','SMW01');
		
		if(!empty($statime) && empty($shoptime)){
				$where['add_time'] = array('egt',$statime);
		}elseif(!empty($statime) && !empty($shoptime)){
				$where['add_time'] = array(array('egt',$statime),array('elt',$shoptime),'and');
		}
		if(!empty($uname) && $sfield!='all'){
				$where[$sfield] = array('like',"%{$uname}%");
		}
		if($_REQUEST['isguide']){
			$path = $this->guide_profit_out($where);
			$this->_ajm(300,'下载地址: <a href='.$path.'>点击下载excel</a>',0);
		}
		$num = $user->join('AS u INNER JOIN user_type AS t ON u.id=t.user_id INNER JOIN `order` AS o ON u.id = o.userid INNER JOIN `order_real_pay_panyu` AS p ON o.id=p.order_id')->group('p.order_id')->where($where)->select();
		$count = count($num);
		//echo $user->getLastSql();
		$Page = new Page( $count,$this->listrows);
		$result = $user->field('u.id,o.id as oid,t.agent_id,o.status,o.pay_status,o.shipping_status,o.add_time,u.username,o.sn,o.amount,sum(p.priceall) as priceall,sum(p.cost_price) as chengben,sum(p.gross_profit) as maolirun,sum(p.profit) as lirun')->join('AS u INNER JOIN user_type AS t ON u.id=t.user_id INNER JOIN `order` AS o ON u.id = o.userid INNER JOIN `order_real_pay_panyu` AS p ON o.id=p.order_id')->where($where)->limit($Page->firstRow.','.$Page->listRows)->group('p.order_id')->order('o.add_time DESC')->select();
		$show  = $Page->show();
		$user = 'user_profit';
		$this->assign($_REQUEST);
		$this->assign('datetime',$statime);
		$this->assign('endtime',$shoptime);
		$this->assign('uname',$uname);
		$this->assign('wherefields',$sfield);
		$this->assign('subject',$status);
		$this->assign('list',$result);
		$this->assign('totalCount',$count);	
		$this->assign('cur_sidebar',$user);
		$this->display();
	}

/****************************
 * 导出番禺区结算数据
 ****************************/
	 function guide_profit_out($where = array()){
	 	import("Com.Excel.Guide");
	 	$user =M('user');
	 	$guide = new Guide();
	 	$arr_column = array('a'=>8,'b'=>15,'c'=>10,'d'=>15,'e'=>15,'f'=>15,'g'=>15,'h'=>10,'i'=>10,'j'=>20);
	 	$arr_title=array('ID','用户帐号','专营商ID','订单编号','订单总额','实际支付','成本','毛利润','分成','下单时间');
		$result = $user->field("u.id,u.username,t.agent_id,o.sn,o.amount/100,sum(p.priceall/100) as priceall,sum(p.cost_price/100) as chengben,sum(p.gross_profit/100) as maolirun,sum(p.profit/100) as lirun,FROM_UNIXTIME(o.add_time ,'%Y-%m-%d %H:%i:%s')")->join('AS u INNER JOIN user_type AS t ON u.id=t.user_id INNER JOIN `order` AS o ON u.id = o.userid INNER JOIN `order_real_pay_panyu` AS p ON o.id=p.order_id')->where($where)->group('p.order_id')->order('o.add_time DESC')->select();
	 	//dump($result);exit;
	 	return $guide->guide_out_data('panyuqu_order_profit', $arr_column, null, $arr_title, $result);
	 }
 
	function price_profit($data){
		$where = array();
		if(!empty($data)){
			$where['agent_id'] = $data;
		}
		$profit = M('UserAgentProfit');
		$res = $profit->where($where)->select();
		if($res){
			return $res;
		}else{
			return false;
		}
	}
}

?>